from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.translation import gettext_lazy as _
from .models import User, UserProfile


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    """用户管理"""
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_verified', 'learning_level', 'is_staff', 'date_joined')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'is_verified', 'learning_level', 'gender')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('-date_joined',)
    
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {
            'fields': ('first_name', 'last_name', 'email', 'phone', 'avatar', 'bio', 'birth_date', 'gender')
        }),
        (_('Learning'), {
            'fields': ('learning_level', 'preferred_language')
        }),
        (_('Permissions'), {
            'fields': ('is_active', 'is_staff', 'is_superuser', 'is_verified', 'groups', 'user_permissions'),
        }),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'email', 'password1', 'password2'),
        }),
    )


@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    """用户资料管理"""
    list_display = ('user', 'total_study_time', 'study_streak', 'last_study_date', 'notification_enabled')
    list_filter = ('learning_time_preference', 'notification_enabled', 'email_notification', 'study_reminder')
    search_fields = ('user__username', 'user__email')
    readonly_fields = ('total_study_time', 'study_streak', 'last_study_date', 'created_at', 'updated_at')
    
    fieldsets = (
        ('学习偏好', {
            'fields': ('user', 'learning_goals', 'interests', 'learning_time_preference')
        }),
        ('统计信息', {
            'fields': ('total_study_time', 'study_streak', 'last_study_date'),
            'classes': ('collapse',)
        }),
        ('通知设置', {
            'fields': ('notification_enabled', 'email_notification', 'study_reminder')
        }),
        ('时间戳', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )